package org.spring.springboot.app.hard.hardinfo.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spring.springboot.app.base.R;
import org.spring.springboot.app.community.structurehard.dao.CommunityStructureHardDao;
import org.spring.springboot.app.community.structurehard.domain.po.CommunityStructureHardPO;
import org.spring.springboot.app.hard.hardinfo.dao.HardInfoDao;
import org.spring.springboot.app.hard.hardinfo.domain.po.HardInfoPO;
import org.spring.springboot.app.hard.hardinfo.domain.vo.*;
import org.spring.springboot.app.hard.hardinfo.service.IHardInfoService;
import org.spring.springboot.app.hard.hardregister.domain.po.HardRegisterPO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class HardInfoServiceImpl implements IHardInfoService {

    private static final Logger LOGGER = LoggerFactory.getLogger(HardInfoServiceImpl.class);

    @Autowired
    private HardInfoDao hardInfoDao;
    @Autowired
    private CommunityStructureHardDao structureHardDao;


    @Override
    public HardInfoPO find(String id) {
        return hardInfoDao.selectByPrimaryKey(id);
    }

    @Override
    public List<HardInfoPO> findAll(String officeId, String types) {
        return hardInfoDao.findAll(officeId,types);
    }

    @Override
    public List<SyshardinfoVO> findPage(SyshardinfoSearchVO searchvo, int pageNum, int pageSize) {
        return hardInfoDao.findPage(searchvo,pageNum, pageSize);
    }

    @Override
    public SyshardinfoVO findSigle(String id) {
        return hardInfoDao.findSigle(id);
    }

    @Override
    public int save(HardInfoPO record) {
        record.preInsert();
        return hardInfoDao.insertSelective(record);
    }

    @Override
    public int update(HardInfoPO record) {
        record.preUpdate();
        return hardInfoDao.updateByPrimaryKeySelective(record);
    }

    @Override
    public int delete(String id) {
        HardInfoPO record = new HardInfoPO();
        record.setId(id);
        record.preDelete();
        return hardInfoDao.updateByPrimaryKeySelective(record);
    }

    @Override
    public int deletes(String ids) {
        HardInfoPO record = new HardInfoPO();
        String[] id_s = ids.split(",");
        record.preDelete();
        int count = 0;
        for (String id : id_s) {
            record.setId(id);
            count += hardInfoDao.updateByPrimaryKeySelective(record);
        }
        return count;
    }



    @Override
    public R saveInfo(SyshardinfosaveVO syshardinfosaveVO) {
        R r = new R();

        //判断注册表中是否有该设备
        HardRegisterPO dr = hardInfoDao.deviceRegister(syshardinfosaveVO.getHardequiid(),null);
        //判断设备号是否存在
        HardInfoPO old =null;
        if(dr!=null){  //判断设备表中是否添加了该设备
             old=hardInfoDao.queryByHardEquiId(dr.getDeviceHardId());  //根据硬件id判断是否已经存在
        }else{
            old=hardInfoDao.queryByHardEquiId(syshardinfosaveVO.getHardequiid());  //根据硬件id判断是否已经存在
        }
        if (old != null) {
            return  r.fail().setMessage("该设备序列已存在");
        }
        //保存设备表
        HardInfoPO po = new HardInfoPO();
        if (dr != null) {  //如果不为空则从新设置，为空则反之
            po.setHardequiid(dr.getDeviceHardId());
        }
        BeanUtils.copyProperties(syshardinfosaveVO, po);
        po.preInsert();
        if (hardInfoDao.insertSelective(po) < 1) {
            return r.fail().setMessage("保存设备表失败");
        }
        //如果关联了小区接点
        if (StringUtils.isNotBlank(syshardinfosaveVO.getRelateId())) {
            CommunityStructureHardPO hardPO = new CommunityStructureHardPO();
            hardPO.setHardId(po.getId());
            hardPO.setStructureId(syshardinfosaveVO.getRelateId());
            if (structureHardDao.insert(hardPO) < 1) {
                return r.fail().setMessage("保存关联表失败");
            }
        }
        return r.success();
    }

    @Override
    public R updateInfo(SyshardinfoUpdateVO syshardinfoUpdateVO) {
        R r = new R();

        //判断注册表中是否有该设备
        HardRegisterPO dr = hardInfoDao.deviceRegister(syshardinfoUpdateVO.getHardequiid(),null);
        //判断设备号是否存在
        HardInfoPO po  = hardInfoDao.selectByPrimaryKey(syshardinfoUpdateVO.getId());
        if (po == null) {
            return r.fail().setMessage("设备不存在");
        }
        //设备号需要修改
        if (!po.getHardequiid().equals(syshardinfoUpdateVO.getHardequiid())) {
            //判断设备号是否存在
            HardInfoPO old =null;
            if(dr!=null){
                old=hardInfoDao.queryByHardEquiId(dr.getDeviceHardId());  //根据硬件id判断是否已经存在
            }else{
                old=hardInfoDao.queryByHardEquiId(syshardinfoUpdateVO.getHardequiid());  //根据硬件id判断是否已经存在
            }
            if (old != null) {
                return  r.fail().setMessage("该设备序列已存在");
            }
        }
        //保存设备表
        po = new HardInfoPO();
        if (dr != null) {
            po.setHardequiid(dr.getDeviceHardId());
        }
        BeanUtils.copyProperties(syshardinfoUpdateVO, po);
        po.preUpdate();
        if (hardInfoDao.updateByPrimaryKeySelective(po) < 1) {
            return r.fail().setMessage("保存设备表失败");
        }
        //删除设备关联的小区节点
        structureHardDao.deleteAllByHardId(syshardinfoUpdateVO.getId());
        //如果关联了小区节点
        if (StringUtils.isNotBlank(syshardinfoUpdateVO.getRelateId())) {
            CommunityStructureHardPO hardPO = new CommunityStructureHardPO();
            hardPO.setHardId(po.getId());
            hardPO.setStructureId(syshardinfoUpdateVO.getRelateId());
            if (structureHardDao.insert(hardPO) < 1) {
                return r.fail().setMessage("保存关联表失败");
            }
        }
        return r.success();
    }

    @Override
    public List<SyshardinfoVO> findPageAgent(SyshardinfoSearchVO searchvo, int pageNum, int pageSize, String officeId) {
        return hardInfoDao.findPageAgent(searchvo,pageNum, pageSize,officeId);
    }

}
